package com.xy.annotaion.handler.servlet3.initializer;

import com.xy.annotaion.handler.common.log.LogService;
import lombok.SneakyThrows;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.util.Objects;
import java.util.Set;

/**
 * TestMyWebInitializer 子类实现类会自动被扫描到，但是不会自动执行，
 * 需要在此接口中主动调用其方法
 *
 * @author Dell
 */
@HandlesTypes({TestMyWebInitializer.class})
public class TestServletContainerInitializerImpl implements ServletContainerInitializer, LogService {

    @SneakyThrows
    @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        //cannot load to use 3rd libs not contained!!!
        log(c);
        log(ctx);
        if (Objects.nonNull(c) && !c.isEmpty()) {
            for (Class<?> c1 : c) {
                TestMyWebInitializer testMyWebInitializer = (TestMyWebInitializer) c1.newInstance();
                testMyWebInitializer.onStartup(ctx);
            }
        }
    }
}
